<template>
  <div>
    <el-button
        v-if="!isShowUpload"
        class="banner-add"
        type="warning"
        @click="isShowUpload = true"
    >添加轮播广告
    </el-button>
    <el-row>
      <div v-if="isShowUpload">
        <el-upload
            :before-remove="beforeRemove"
            :file-list="fileList"
            :http-request="handleUpload"
            :limit="1"
            :on-exceed="handleExceed"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            action
            class="upload-demo"
            list-type="picture"
            multiple
            name="singleFile"
        >
          <el-button size="small" type="warning">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">一次最多上传一张简报</div>
        </el-upload>
        <el-select v-model="params.entityId" placeholder="请选择对应简报" @change="handleChangeType" clearable>
          <el-option
              v-for="(item) in types"
              :key="item.activitycontentId"
              :label="item.activitycontentTitle"
              :value="item.activitycontentId"
              clearable
          >
            <span style="float: left">{{ item.activitycontentTitle }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.activitycontentDatetime }}</span>
          </el-option>
        </el-select>
        <el-button
            class="banner-add-now"
            size="small"
            type="warning"
            @click.prevent="handleAddBanner"
        >立即添加
        </el-button>
      </div>
    </el-row>

    <el-table :data="bannerList" border max-height="680" style="width: 100%">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.imageUrl" alt="轮播图片" class="banner-img" height="100" width="100"/>
        </template>
      </el-table-column>
      <el-table-column
          label="简报标题"
          prop="type"
      ></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="danger" @click="handleChange(scope.row.id)"
          >删除简报
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>

export default {
  data () {
    return {
      bannerList: [],
      isShowUpload: false,
      fileList: [],
      realFileUrl: [],
      types: [],
      params: {
        entityId: "",
        id: "",
        imageUrl: "",
        title: "",
        type: "",
      },
    };
  },
  mounted () {
    this.handleGetBannerList();
    this.handleGetPosterArticle();
  },
  methods: {
    handleChangeType (
        res,
    ) {
      let articles = this.types;
      for (let i of articles) {
        if (i.activitycontentId === ~~res) {
          this.params.type = i.activitycontentTitle;
        }
      }
    },
    //   查询广告
    async handleGetBannerList () {
      const res = await this.$http.posterListGet();
      if (res.code !== 0) return null;
      // console.log(res)
      this.bannerList = res.data;
    },
    async handleGetPosterArticle () {
      let res = await this.$http.activityContentGet({
        pageNum: 1,
        pageSize: 9999,
      });
      this.types = res.data.list;
      // console.log(res)
    },
    // 删除广告信息
    async handleChange (e) {
      // console.log(e)
      const res = await this.$http.posterDel(e);
      if (res.code !== 0) return null;
      this.$message.success("删除成功");
      this.$router.go(0);
    },

    // 添加轮播广告信息
    async handleAddBanner () {
      if (this.realFileUrl.length === 0) return this.$message.error("请选择图片");
      if (this.params.type === "") return this.$message.error("请选择类型");
      const imgUrl = this.realFileUrl.join(",");
      const params = this.params;
      params.imageUrl = imgUrl;
      const res = await this.$http.posterPost(params);
      if (res.code !== 0) return null;
      this.$message.success("添加成功");
      this.$router.go(0);
    },

    handleRemove () {
      this.isShowUpload = !this.isShowUpload;
      // console.log(file, fileList)
    },
    handlePreview (file) {
      // console.log(file)
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 4 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove (file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 上传文件
    async handleUpload (file) {
      const params = new FormData();
      params.append("files", file.file);
      const res = await this.$http.uploadFile(params);
      if (res.code !== 200) return this.$message.error(res.message);
      this.realFileUrl.push(res.files[0]);
    },
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 20px;
}

.banner-add {
  margin-bottom: 20px;
}

.banner-add-now {
  margin-top: 40px;
  margin-bottom: 20px;
}

.banner-img {
  width: 200px;
}
</style>
